package com.gc.server.service.impl;

import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gc.server.dao.PlanDao;
import com.gc.server.entity.PlanEntity;
import com.gc.server.service.PlanService;
import com.yf.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 周计划Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Mar 10 11:17:45 CST 2020
 */
@Service
public class PlanServiceImpl extends BaseServiceImpl<PlanEntity> implements PlanService {

    private PlanDao planDao;


    @Autowired
    public void setDao(PlanDao planDao) {
        this.planDao = planDao;
        super.setBaseDao(planDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override
     List<PlanVo> queryListVo(PlanVo planVo){
            return planDao.queryListVo(planVo);
     }

     @Override
     PlanVo getVo(Long id){
                return planDao.getVo(id);
    }

     **/


    /**内部方法**/



    /**外部方法实现**/

    /**
     * 新增一个为期六天的周计划
     * @param userId
     * @param startDate
     * @return
     */
    @Override
    @Transactional
    public PlanEntity add(Long userId, Date startDate,Date endDate,int year,int week) {
//        LocalDate start = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//        LocalDate end = start.plusDays(6);
//        PlanEntity planEntity = new PlanEntity(userId,Date.from(start.atStartOfDay(ZoneId.systemDefault()).toInstant()),Date.from(end.atStartOfDay(ZoneId.systemDefault()).toInstant()),new Date(),title);
//        if(new Date().before(startDate)){
//            throw new RRException("当前周尚未开始!");
//        }
        if(new Date().after(endDate)){
            throw new RRException("当前周已经结束!");
        }
        PlanEntity planEntity = new PlanEntity(userId,startDate,endDate,new Date(),year,week);
        save(planEntity);
        return planEntity;
    }

    @Override
    public List<PlanEntity> list(Long userId) {
        return planDao.findByUserId(userId);
    }

    @Override
    public List<PlanEntity> findByYear(Long userId,int year) {
        return planDao.findByYear(userId,year);
    }
}
